﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.IO;

namespace HW_h1
{
    // 学校实体类
    public class School
    {
        public int SchoolId { get; set; }
        public string SchoolName { get; set; }
        public string Address { get; set; }

        public override string ToString()
        {
            return $"ID: {SchoolId}, 学校名称: {SchoolName}, 地址: {Address}";
        }
    }

    // 班级实体类
    public class Class
    {
        public int ClassId { get; set; }
        public string ClassName { get; set; }
        public int SchoolId { get; set; } // 外键关联学校

        public override string ToString()
        {
            return $"ID: {ClassId}, 班级名称: {ClassName}, 所属学校ID: {SchoolId}";
        }
    }

    // 学生实体类
    public class Student
    {
        public int StudentId { get; set; }
        public string StudentName { get; set; }
        public int Age { get; set; }
        public string Gender { get; set; }
        public int ClassId { get; set; } // 外键关联班级

        public override string ToString()
        {
            return $"ID: {StudentId}, 学生姓名: {StudentName}, 年龄: {Age}, 性别: {Gender}, 所属班级ID: {ClassId}";
        }
    }

    // 数据库管理类
    public class DatabaseManager
    {
        private static string dbPath = "StudentManagement.db";
        private static string connectionString = $"Data Source={dbPath};Version=3;";

        // 初始化数据库
        public static void InitializeDatabase()
        {
            // 如果数据库文件不存在，则创建
            if (!File.Exists(dbPath))
            {
                SQLiteConnection.CreateFile(dbPath);
            }

            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                connection.Open();

                // 创建学校表
                string createSchoolTable = @"
                    CREATE TABLE IF NOT EXISTS School (
                        SchoolId INTEGER PRIMARY KEY AUTOINCREMENT,
                        SchoolName TEXT NOT NULL,
                        Address TEXT
                    );";

                // 创建班级表
                string createClassTable = @"
                    CREATE TABLE IF NOT EXISTS Class (
                        ClassId INTEGER PRIMARY KEY AUTOINCREMENT,
                        ClassName TEXT NOT NULL,
                        SchoolId INTEGER,
                        FOREIGN KEY (SchoolId) REFERENCES School(SchoolId)
                    );";

                // 创建学生表
                string createStudentTable = @"
                    CREATE TABLE IF NOT EXISTS Student (
                        StudentId INTEGER PRIMARY KEY AUTOINCREMENT,
                        StudentName TEXT NOT NULL,
                        Age INTEGER,
                        Gender TEXT,
                        ClassId INTEGER,
                        FOREIGN KEY (ClassId) REFERENCES Class(ClassId)
                    );";

                // 创建日志表
                string createLogTable = @"
                    CREATE TABLE IF NOT EXISTS Log (
                        LogId INTEGER PRIMARY KEY AUTOINCREMENT,
                        OperationType TEXT NOT NULL,
                        OperationDetails TEXT,
                        Timestamp TEXT DEFAULT CURRENT_TIMESTAMP
                    );";

                using (SQLiteCommand command = new SQLiteCommand(connection))
                {
                    command.CommandText = createSchoolTable;
                    command.ExecuteNonQuery();

                    command.CommandText = createClassTable;
                    command.ExecuteNonQuery();

                    command.CommandText = createStudentTable;
                    command.ExecuteNonQuery();

                    command.CommandText = createLogTable;
                    command.ExecuteNonQuery();
                }
            }
        }

        // 记录操作日志
        private static void LogOperation(string operationType, string operationDetails)
        {
            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                string query = "INSERT INTO Log (OperationType, OperationDetails) VALUES (@OperationType, @OperationDetails)";

                using (SQLiteCommand command = new SQLiteCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@OperationType", operationType);
                    command.Parameters.AddWithValue("@OperationDetails", operationDetails);
                    command.ExecuteNonQuery();
                }
            }
        }

        #region 学校管理
        // 添加学校
        public static int AddSchool(string schoolName, string address)
        {
            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                string query = "INSERT INTO School (SchoolName, Address) VALUES (@SchoolName, @Address); SELECT last_insert_rowid();";

                using (SQLiteCommand command = new SQLiteCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@SchoolName", schoolName);
                    command.Parameters.AddWithValue("@Address", address);
                    int newId = Convert.ToInt32(command.ExecuteScalar());

                    LogOperation("添加学校", $"学校名称: {schoolName}, 地址: {address}");
                    return newId;
                }
            }
        }

        // 更新学校信息
        public static bool UpdateSchool(int schoolId, string schoolName, string address)
        {
            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                string query = "UPDATE School SET SchoolName = @SchoolName, Address = @Address WHERE SchoolId = @SchoolId";

                using (SQLiteCommand command = new SQLiteCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@SchoolId", schoolId);
                    command.Parameters.AddWithValue("@SchoolName", schoolName);
                    command.Parameters.AddWithValue("@Address", address);
                    int rowsAffected = command.ExecuteNonQuery();

                    if (rowsAffected > 0)
                    {
                        LogOperation("更新学校", $"学校ID: {schoolId}, 新名称: {schoolName}, 新地址: {address}");
                        return true;
                    }
                    return false;
                }
            }
        }

        // 删除学校
        public static bool DeleteSchool(int schoolId)
        {
            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                string query = "DELETE FROM School WHERE SchoolId = @SchoolId";

                using (SQLiteCommand command = new SQLiteCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@SchoolId", schoolId);
                    int rowsAffected = command.ExecuteNonQuery();

                    if (rowsAffected > 0)
                    {
                        LogOperation("删除学校", $"学校ID: {schoolId}");
                        return true;
                    }
                    return false;
                }
            }
        }

        // 获取所有学校
        public static List<School> GetAllSchools()
        {
            List<School> schools = new List<School>();

            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                string query = "SELECT * FROM School";

                using (SQLiteCommand command = new SQLiteCommand(query, connection))
                {
                    using (SQLiteDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            School school = new School
                            {
                                SchoolId = Convert.ToInt32(reader["SchoolId"]),
                                SchoolName = reader["SchoolName"].ToString(),
                                Address = reader["Address"].ToString()
                            };
                            schools.Add(school);
                        }
                    }
                }

                LogOperation("查询学校", "获取所有学校信息");
            }

            return schools;
        }

        // 获取指定学校
        public static School GetSchoolById(int schoolId)
        {
            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                string query = "SELECT * FROM School WHERE SchoolId = @SchoolId";

                using (SQLiteCommand command = new SQLiteCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@SchoolId", schoolId);

                    using (SQLiteDataReader reader = command.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            School school = new School
                            {
                                SchoolId = Convert.ToInt32(reader["SchoolId"]),
                                SchoolName = reader["SchoolName"].ToString(),
                                Address = reader["Address"].ToString()
                            };

                            LogOperation("查询学校", $"获取学校ID: {schoolId} 的信息");
                            return school;
                        }
                    }
                }
            }

            return null;
        }
        #endregion

        #region 班级管理
        // 添加班级
        public static int AddClass(string className, int schoolId)
        {
            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                string query = "INSERT INTO Class (ClassName, SchoolId) VALUES (@ClassName, @SchoolId); SELECT last_insert_rowid();";

                using (SQLiteCommand command = new SQLiteCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@ClassName", className);
                    command.Parameters.AddWithValue("@SchoolId", schoolId);
                    int newId = Convert.ToInt32(command.ExecuteScalar());

                    LogOperation("添加班级", $"班级名称: {className}, 所属学校ID: {schoolId}");
                    return newId;
                }
            }
        }

        // 更新班级信息
        public static bool UpdateClass(int classId, string className, int schoolId)
        {
            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                string query = "UPDATE Class SET ClassName = @ClassName, SchoolId = @SchoolId WHERE ClassId = @ClassId";

                using (SQLiteCommand command = new SQLiteCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@ClassId", classId);
                    command.Parameters.AddWithValue("@ClassName", className);
                    command.Parameters.AddWithValue("@SchoolId", schoolId);
                    int rowsAffected = command.ExecuteNonQuery();

                    if (rowsAffected > 0)
                    {
                        LogOperation("更新班级", $"班级ID: {classId}, 新名称: {className}, 所属学校ID: {schoolId}");
                        return true;
                    }
                    return false;
                }
            }
        }

        // 删除班级
        public static bool DeleteClass(int classId)
        {
            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                string query = "DELETE FROM Class WHERE ClassId = @ClassId";

                using (SQLiteCommand command = new SQLiteCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@ClassId", classId);
                    int rowsAffected = command.ExecuteNonQuery();

                    if (rowsAffected > 0)
                    {
                        LogOperation("删除班级", $"班级ID: {classId}");
                        return true;
                    }
                    return false;
                }
            }
        }

        // 获取所有班级
        public static List<Class> GetAllClasses()
        {
            List<Class> classes = new List<Class>();

            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                string query = "SELECT * FROM Class";

                using (SQLiteCommand command = new SQLiteCommand(query, connection))
                {
                    using (SQLiteDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            Class cls = new Class
                            {
                                ClassId = Convert.ToInt32(reader["ClassId"]),
                                ClassName = reader["ClassName"].ToString(),
                                SchoolId = Convert.ToInt32(reader["SchoolId"])
                            };
                            classes.Add(cls);
                        }
                    }
                }

                LogOperation("查询班级", "获取所有班级信息");
            }

            return classes;
        }

        // 获取指定班级
        public static Class GetClassById(int classId)
        {
            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                string query = "SELECT * FROM Class WHERE ClassId = @ClassId";

                using (SQLiteCommand command = new SQLiteCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@ClassId", classId);

                    using (SQLiteDataReader reader = command.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            Class cls = new Class
                            {
                                ClassId = Convert.ToInt32(reader["ClassId"]),
                                ClassName = reader["ClassName"].ToString(),
                                SchoolId = Convert.ToInt32(reader["SchoolId"])
                            };

                            LogOperation("查询班级", $"获取班级ID: {classId} 的信息");
                            return cls;
                        }
                    }
                }
            }

            return null;
        }

        // 获取学校下的所有班级
        public static List<Class> GetClassesBySchoolId(int schoolId)
        {
            List<Class> classes = new List<Class>();

            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                string query = "SELECT * FROM Class WHERE SchoolId = @SchoolId";

                using (SQLiteCommand command = new SQLiteCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@SchoolId", schoolId);

                    using (SQLiteDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            Class cls = new Class
                            {
                                ClassId = Convert.ToInt32(reader["ClassId"]),
                                ClassName = reader["ClassName"].ToString(),
                                SchoolId = Convert.ToInt32(reader["SchoolId"])
                            };
                            classes.Add(cls);
                        }
                    }
                }

                LogOperation("查询班级", $"获取学校ID: {schoolId} 下的所有班级");
            }

            return classes;
        }
        #endregion

        #region 学生管理
        // 添加学生
        public static int AddStudent(string studentName, int age, string gender, int classId)
        {
            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                string query = "INSERT INTO Student (StudentName, Age, Gender, ClassId) VALUES (@StudentName, @Age, @Gender, @ClassId); SELECT last_insert_rowid();";

                using (SQLiteCommand command = new SQLiteCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@StudentName", studentName);
                    command.Parameters.AddWithValue("@Age", age);
                    command.Parameters.AddWithValue("@Gender", gender);
                    command.Parameters.AddWithValue("@ClassId", classId);
                    int newId = Convert.ToInt32(command.ExecuteScalar());

                    LogOperation("添加学生", $"学生姓名: {studentName}, 年龄: {age}, 性别: {gender}, 所属班级ID: {classId}");
                    return newId;
                }
            }
        }

        // 更新学生信息
        public static bool UpdateStudent(int studentId, string studentName, int age, string gender, int classId)
        {
            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                string query = "UPDATE Student SET StudentName = @StudentName, Age = @Age, Gender = @Gender, ClassId = @ClassId WHERE StudentId = @StudentId";

                using (SQLiteCommand command = new SQLiteCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@StudentId", studentId);
                    command.Parameters.AddWithValue("@StudentName", studentName);
                    command.Parameters.AddWithValue("@Age", age);
                    command.Parameters.AddWithValue("@Gender", gender);
                    command.Parameters.AddWithValue("@ClassId", classId);
                    int rowsAffected = command.ExecuteNonQuery();

                    if (rowsAffected > 0)
                    {
                        LogOperation("更新学生", $"学生ID: {studentId}, 新姓名: {studentName}, 新年龄: {age}, 新性别: {gender}, 所属班级ID: {classId}");
                        return true;
                    }
                    return false;
                }
            }
        }

        // 删除学生
        public static bool DeleteStudent(int studentId)
        {
            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                string query = "DELETE FROM Student WHERE StudentId = @StudentId";

                using (SQLiteCommand command = new SQLiteCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@StudentId", studentId);
                    int rowsAffected = command.ExecuteNonQuery();

                    if (rowsAffected > 0)
                    {
                        LogOperation("删除学生", $"学生ID: {studentId}");
                        return true;
                    }
                    return false;
                }
            }
        }

        // 获取所有学生
        public static List<Student> GetAllStudents()
        {
            List<Student> students = new List<Student>();

            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                string query = "SELECT * FROM Student";

                using (SQLiteCommand command = new SQLiteCommand(query, connection))
                {
                    using (SQLiteDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            Student student = new Student
                            {
                                StudentId = Convert.ToInt32(reader["StudentId"]),
                                StudentName = reader["StudentName"].ToString(),
                                Age = Convert.ToInt32(reader["Age"]),
                                Gender = reader["Gender"].ToString(),
                                ClassId = Convert.ToInt32(reader["ClassId"])
                            };
                            students.Add(student);
                        }
                    }
                }

                LogOperation("查询学生", "获取所有学生信息");
            }

            return students;
        }

        // 获取指定学生
        public static Student GetStudentById(int studentId)
        {
            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                string query = "SELECT * FROM Student WHERE StudentId = @StudentId";

                using (SQLiteCommand command = new SQLiteCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@StudentId", studentId);

                    using (SQLiteDataReader reader = command.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            Student student = new Student
                            {
                                StudentId = Convert.ToInt32(reader["StudentId"]),
                                StudentName = reader["StudentName"].ToString(),
                                Age = Convert.ToInt32(reader["Age"]),
                                Gender = reader["Gender"].ToString(),
                                ClassId = Convert.ToInt32(reader["ClassId"])
                            };

                            LogOperation("查询学生", $"获取学生ID: {studentId} 的信息");
                            return student;
                        }
                    }
                }
            }

            return null;
        }

        // 获取班级下的所有学生
        public static List<Student> GetStudentsByClassId(int classId)
        {
            List<Student> students = new List<Student>();

            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                string query = "SELECT * FROM Student WHERE ClassId = @ClassId";

                using (SQLiteCommand command = new SQLiteCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@ClassId", classId);

                    using (SQLiteDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            Student student = new Student
                            {
                                StudentId = Convert.ToInt32(reader["StudentId"]),
                                StudentName = reader["StudentName"].ToString(),
                                Age = Convert.ToInt32(reader["Age"]),
                                Gender = reader["Gender"].ToString(),
                                ClassId = Convert.ToInt32(reader["ClassId"])
                            };
                            students.Add(student);
                        }
                    }
                }

                LogOperation("查询学生", $"获取班级ID: {classId} 下的所有学生");
            }

            return students;
        }
        #endregion

        #region 日志管理
        // 获取所有日志
        public static List<string> GetAllLogs()
        {
            List<string> logs = new List<string>();

            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                string query = "SELECT * FROM Log ORDER BY Timestamp DESC";

                using (SQLiteCommand command = new SQLiteCommand(query, connection))
                {
                    using (SQLiteDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            string log = $"[{reader["Timestamp"]}] {reader["OperationType"]}: {reader["OperationDetails"]}";
                            logs.Add(log);
                        }
                    }
                }
            }

            return logs;
        }
        #endregion
    }

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                // 初始化数据库
                DatabaseManager.InitializeDatabase();
                Console.WriteLine("数据库初始化完成\n");

                while (true)
                {
                    Console.WriteLine("================ 学生管理系统 ================");
                    Console.WriteLine("1. 学校管理");
                    Console.WriteLine("2. 班级管理");
                    Console.WriteLine("3. 学生管理");
                    Console.WriteLine("4. 查看日志");
                    Console.WriteLine("0. 退出系统");
                    Console.WriteLine("============================================");
                    Console.Write("请输入您的选择 (0-4): ");

                    string choice = Console.ReadLine();
                    Console.WriteLine();

                    switch (choice)
                    {
                        case "1":
                            ManageSchools();
                            break;
                        case "2":
                            ManageClasses();
                            break;
                        case "3":
                            ManageStudents();
                            break;
                        case "4":
                            ShowLogs();
                            break;
                        case "0":
                            Console.WriteLine("感谢使用，再见！");
                            return;
                        default:
                            Console.WriteLine("无效的选择，请重试。");
                            break;
                    }

                    Console.WriteLine("\n按任意键继续...");
                    Console.ReadKey();
                    Console.Clear();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"发生错误: {ex.Message}");
                Console.ReadKey();
            }
        }

        static void ManageSchools()
        {
            while (true)
            {
                Console.WriteLine("================ 学校管理 ================");
                Console.WriteLine("1. 添加学校");
                Console.WriteLine("2. 修改学校");
                Console.WriteLine("3. 删除学校");
                Console.WriteLine("4. 查看所有学校");
                Console.WriteLine("0. 返回上级菜单");
                Console.WriteLine("=======================================");
                Console.Write("请输入您的选择 (0-4): ");

                string choice = Console.ReadLine();
                Console.WriteLine();

                switch (choice)
                {
                    case "1":
                        Console.Write("请输入学校名称: ");
                        string schoolName = Console.ReadLine();
                        Console.Write("请输入学校地址: ");
                        string address = Console.ReadLine();
                        int schoolId = DatabaseManager.AddSchool(schoolName, address);
                        Console.WriteLine($"添加学校成功，ID: {schoolId}");
                        break;

                    case "2":
                        Console.Write("请输入要修改的学校ID: ");
                        if (int.TryParse(Console.ReadLine(), out int updateSchoolId))
                        {
                            Console.Write("请输入新的学校名称: ");
                            string newSchoolName = Console.ReadLine();
                            Console.Write("请输入新的学校地址: ");
                            string newAddress = Console.ReadLine();
                            bool updated = DatabaseManager.UpdateSchool(updateSchoolId, newSchoolName, newAddress);
                            Console.WriteLine(updated ? "修改成功！" : "修改失败，未找到指定学校。");
                        }
                        break;

                    case "3":
                        Console.Write("请输入要删除的学校ID: ");
                        if (int.TryParse(Console.ReadLine(), out int deleteSchoolId))
                        {
                            bool deleted = DatabaseManager.DeleteSchool(deleteSchoolId);
                            Console.WriteLine(deleted ? "删除成功！" : "删除失败，未找到指定学校。");
                        }
                        break;

                    case "4":
                        var schools = DatabaseManager.GetAllSchools();
                        if (schools.Count == 0)
                        {
                            Console.WriteLine("暂无学校信息。");
                        }
                        else
                        {
                            foreach (var school in schools)
                            {
                                Console.WriteLine(school);
                            }
                        }
                        break;

                    case "0":
                        return;

                    default:
                        Console.WriteLine("无效的选择，请重试。");
                        break;
                }

                Console.WriteLine("\n按任意键继续...");
                Console.ReadKey();
                Console.Clear();
            }
        }

        static void ManageClasses()
        {
            while (true)
            {
                Console.WriteLine("================ 班级管理 ================");
                Console.WriteLine("1. 添加班级");
                Console.WriteLine("2. 修改班级");
                Console.WriteLine("3. 删除班级");
                Console.WriteLine("4. 查看所有班级");
                Console.WriteLine("5. 查看指定学校的班级");
                Console.WriteLine("0. 返回上级菜单");
                Console.WriteLine("=======================================");
                Console.Write("请输入您的选择 (0-5): ");

                string choice = Console.ReadLine();
                Console.WriteLine();

                switch (choice)
                {
                    case "1":
                        Console.Write("请输入班级名称: ");
                        string className = Console.ReadLine();
                        Console.Write("请输入所属学校ID: ");
                        if (int.TryParse(Console.ReadLine(), out int schoolId))
                        {
                            int classId = DatabaseManager.AddClass(className, schoolId);
                            Console.WriteLine($"添加班级成功，ID: {classId}");
                        }
                        break;

                    case "2":
                        Console.Write("请输入要修改的班级ID: ");
                        if (int.TryParse(Console.ReadLine(), out int updateClassId))
                        {
                            Console.Write("请输入新的班级名称: ");
                            string newClassName = Console.ReadLine();
                            Console.Write("请输入新的所属学校ID: ");
                            if (int.TryParse(Console.ReadLine(), out int newSchoolId))
                            {
                                bool updated = DatabaseManager.UpdateClass(updateClassId, newClassName, newSchoolId);
                                Console.WriteLine(updated ? "修改成功！" : "修改失败，未找到指定班级。");
                            }
                        }
                        break;

                    case "3":
                        Console.Write("请输入要删除的班级ID: ");
                        if (int.TryParse(Console.ReadLine(), out int deleteClassId))
                        {
                            bool deleted = DatabaseManager.DeleteClass(deleteClassId);
                            Console.WriteLine(deleted ? "删除成功！" : "删除失败，未找到指定班级。");
                        }
                        break;

                    case "4":
                        var classes = DatabaseManager.GetAllClasses();
                        if (classes.Count == 0)
                        {
                            Console.WriteLine("暂无班级信息。");
                        }
                        else
                        {
                            foreach (var cls in classes)
                            {
                                Console.WriteLine(cls);
                            }
                        }
                        break;

                    case "5":
                        Console.Write("请输入学校ID: ");
                        if (int.TryParse(Console.ReadLine(), out int querySchoolId))
                        {
                            var schoolClasses = DatabaseManager.GetClassesBySchoolId(querySchoolId);
                            if (schoolClasses.Count == 0)
                            {
                                Console.WriteLine("该学校暂无班级信息。");
                            }
                            else
                            {
                                foreach (var cls in schoolClasses)
                                {
                                    Console.WriteLine(cls);
                                }
                            }
                        }
                        break;

                    case "0":
                        return;

                    default:
                        Console.WriteLine("无效的选择，请重试。");
                        break;
                }

                Console.WriteLine("\n按任意键继续...");
                Console.ReadKey();
                Console.Clear();
            }
        }

        static void ManageStudents()
        {
            while (true)
            {
                Console.WriteLine("================ 学生管理 ================");
                Console.WriteLine("1. 添加学生");
                Console.WriteLine("2. 修改学生信息");
                Console.WriteLine("3. 删除学生");
                Console.WriteLine("4. 查看所有学生");
                Console.WriteLine("5. 查看指定班级的学生");
                Console.WriteLine("0. 返回上级菜单");
                Console.WriteLine("=======================================");
                Console.Write("请输入您的选择 (0-5): ");

                string choice = Console.ReadLine();
                Console.WriteLine();

                switch (choice)
                {
                    case "1":
                        Console.Write("请输入学生姓名: ");
                        string studentName = Console.ReadLine();
                        Console.Write("请输入学生年龄: ");
                        if (int.TryParse(Console.ReadLine(), out int age))
                        {
                            Console.Write("请输入学生性别: ");
                            string gender = Console.ReadLine();
                            Console.Write("请输入所属班级ID: ");
                            if (int.TryParse(Console.ReadLine(), out int classId))
                            {
                                int studentId = DatabaseManager.AddStudent(studentName, age, gender, classId);
                                Console.WriteLine($"添加学生成功，ID: {studentId}");
                            }
                        }
                        break;

                    case "2":
                        Console.Write("请输入要修改的学生ID: ");
                        if (int.TryParse(Console.ReadLine(), out int updateStudentId))
                        {
                            Console.Write("请输入新的学生姓名: ");
                            string newStudentName = Console.ReadLine();
                            Console.Write("请输入新的学生年龄: ");
                            if (int.TryParse(Console.ReadLine(), out int newAge))
                            {
                                Console.Write("请输入新的学生性别: ");
                                string newGender = Console.ReadLine();
                                Console.Write("请输入新的所属班级ID: ");
                                if (int.TryParse(Console.ReadLine(), out int newClassId))
                                {
                                    bool updated = DatabaseManager.UpdateStudent(updateStudentId, newStudentName, newAge, newGender, newClassId);
                                    Console.WriteLine(updated ? "修改成功！" : "修改失败，未找到指定学生。");
                                }
                            }
                        }
                        break;

                    case "3":
                        Console.Write("请输入要删除的学生ID: ");
                        if (int.TryParse(Console.ReadLine(), out int deleteStudentId))
                        {
                            bool deleted = DatabaseManager.DeleteStudent(deleteStudentId);
                            Console.WriteLine(deleted ? "删除成功！" : "删除失败，未找到指定学生。");
                        }
                        break;

                    case "4":
                        var students = DatabaseManager.GetAllStudents();
                        if (students.Count == 0)
                        {
                            Console.WriteLine("暂无学生信息。");
                        }
                        else
                        {
                            foreach (var student in students)
                            {
                                Console.WriteLine(student);
                            }
                        }
                        break;

                    case "5":
                        Console.Write("请输入班级ID: ");
                        if (int.TryParse(Console.ReadLine(), out int queryClassId))
                        {
                            var classStudents = DatabaseManager.GetStudentsByClassId(queryClassId);
                            if (classStudents.Count == 0)
                            {
                                Console.WriteLine("该班级暂无学生信息。");
                            }
                            else
                            {
                                foreach (var student in classStudents)
                                {
                                    Console.WriteLine(student);
                                }
                            }
                        }
                        break;

                    case "0":
                        return;

                    default:
                        Console.WriteLine("无效的选择，请重试。");
                        break;
                }

                Console.WriteLine("\n按任意键继续...");
                Console.ReadKey();
                Console.Clear();
            }
        }

        static void ShowLogs()
        {
            var logs = DatabaseManager.GetAllLogs();
            if (logs.Count == 0)
            {
                Console.WriteLine("暂无操作日志。");
            }
            else
            {
                Console.WriteLine("================ 操作日志 ================");
                foreach (var log in logs)
                {
                    Console.WriteLine(log);
                }
                Console.WriteLine("========================================");
            }
        }
    }

}


